{include file="public/header-static"/}
{include file="public/sidebar"/}


<div id="main">

    {include file="public/header"/}





    <main class="main-content">

        <div class="container">


            <div class="page-header">
                <h4>权限管理</h4>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="#">首页</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">角色设置 </li>
                    </ol>
                </nav>
            </div>
            <!-- end::page-header -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="app-block">
                                <div class="app-content">
                                    <div class="app-action">
                                        <div class="action-left">
                                            <ul class="list-inline">
                                                <li class="list-inline-item mb-0">
                                                    <a href="#" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">
                                                        <i data-feather="plus" class="mr-2"></i>
                                                        添加
                                                    </a>
                                                    <div class="dropdown-menu">
                                                        <a class="dropdown-item" href="{:url('/power/role_edit')}">角色组</a>
                                                    </div>
                                                </li>
                                            </ul>
                                        </div>
                                        <div class="action-right">
                                            <form class="d-flex mr-3" method="get" action="">
                                                <a href="#" class="app-sidebar-menu-button btn btn-outline-light">
                                                    <i data-feather="menu"></i>
                                                </a>
                                                <div class="input-group">
                                                    <input type="text" id="keyname" name="keyname" autofocus="autofocus" value="{$Request.get.keyname}" class="form-control" placeholder="搜索关键字[角色名称]" aria-describedby="button-addon1">
                                                    <div class="input-group-append">
                                                        <button class="btn btn-outline-light" type="submit" id="button-addon1">
                                                            <i data-feather="search"></i>
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <table id="example1" class="table table-striped table-bordered" style="width:100%">
                                <thead class="thead-dark">
                                <tr>
                                    <th>ID</th>
                                    <th>角色名称</th>
                                    <th>上级角色</th>
                                    <th>描述</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>更新时间</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </main>

    {include file="public/footer"/}
</div>


<!-- Plugin scripts -->
<script src="__STATIC__/vendors/bundle.js"></script>

<!-- DataTable -->
<script src="__STATIC__/vendors/dataTable/jquery.dataTables.min.js"></script>
<script src="__STATIC__/vendors/dataTable/dataTables.bootstrap4.min.js"></script>
<script src="__STATIC__/vendors/dataTable/dataTables.responsive.min.js"></script>

<script>

    function role_edit(id) {
        window.location.href = '{:url("/power/role_edit")}?id='+id;
    }
    function role_del(id,that) {
        $.post("{:url('/power/role_del')}", {id:id}, function(data) {
            if(data['code']!=1)
            {
                toastr.error(data['msg']);
            }else{
                $(that).parents('tr.even').remove();
                toastr.success(data['msg']);
                setInterval(function () {
                    window.location.href = '';
                },1000);
            }
        });
    }
    function role_set(id) {
        window.location.href = '{:url("/power/role_set")}?id='+id;
    }


    $(document).ready(function () {
        $('#example1').DataTable({
            lengthChange:false,//是否允许用户改变表格每页显示的记录数
            paging: true,//是否开启本地分页
            pagingType:"simple_numbers",//分页按钮显示选项
            searching:false,//是否允许 DataTables 开启本地搜索
            info:false,//是否显示左下角信息
            ordering:false,
            serverSide: true, // 服务端处理分页
            processing:true,// 数据量大则显示进度
            ajax: function (data, callback, settings) {
                var param = {};
                param.keyname = $.trim($("#keyname").val());//额外传参数
                param.size = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;//开始的记录序号
                param.page = (data.start / data.length)+1;//当前页码
                param.order = data.order[0];
                $.ajax({
                    url:"{:url('/Power/role')}",
                    type:'POST',
                    cache: false,  //禁用缓存
                    data: param,  //传入组装的参数
                    dataType: "json",
                    success: function (result) {
                        setTimeout(function () {
                            //封装返回数据
                            var returnData = {};
                            returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                            returnData.recordsTotal = result.total;//返回数据全部记录
                            returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = result.data;//返回的数据列表
                            callback(returnData);
                        }, 200);
                    }
                })
            },
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示 _START_ 到 _END_，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "从当前数据中检索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
            columns: [
                { data : null,'render':function (data,type,row,meta) {
                        var html = 'ID: '+row['id']+' ';
                        if(row['id'] != 1)
                        {
                            html += '<button type="button" title="编辑" data-toggle="tooltip" data-placement="top" onclick="role_edit('+row['id']+')" class="btn btn-secondary btn-sm" href="#'+row['id']+'"  ><i class="ti-check-box"></i></button> ';
                            html += '<button type="button" title="设置" data-toggle="tooltip" data-placement="top" onclick="role_set('+row['id']+')" class="btn btn-secondary btn-sm" href="#'+row['id']+'"  ><i class="ti-settings"></i></button> ';
                            html += ' <button type="button" title="删除" data-toggle="tooltip" data-placement="top" onclick="role_del('+row['id']+',this)" class="btn btn-secondary btn-sm" href="#'+row['id']+'"  ><i class="ti-trash"></i></button>';
                        }else{
                        }


                        return html;
                    }},
                { data: "name"},
                { data: "pid"},
                { data: "description"},
                { data: null,'render':function (data,type,row,meta) {
                        var html = '<div class="form-group"><div class="custom-control custom-switch custom-checkbox-secondary">';
                        if(row['id'] != 1) {
                            if (row['status'] == 1) {
                                html += '<input type="checkbox" onclick="fieldchange(' + row['id'] + ')" class="custom-control-input" id="customSwitch' + row['id'] + '" checked><label class="custom-control-label" for="customSwitch' + row['id'] + '"></label>';
                            } else {
                                html += '<input type="checkbox" onclick="fieldchange(' + row['id'] + ')" class="custom-control-input" id="customSwitch' + row['id'] + '"><label class="custom-control-label" for="customSwitch' + row['id'] + '"></label>';
                            }
                        }
                        html += '</div></div>';
                        return html;
                    }},
                { data: "create_time"},
                { data: "update_time"}
            ],

            /* 不生效，奇怪了。。
            drawCallback: function(settings) {
                var pagination = $(this).closest('.dataTables_wrapper').find('.dataTables_paginate');
                pagination.toggle(this.api().page.info().pages > 1);
            },
            */
        });
    });
</script>

<!-- Prism -->
<script src="__STATIC__/vendors/prism/prism.js"></script>

<!-- App scripts -->
<script src="__STATIC__/assets/js/app.min.js"></script>
<script>
    function fieldchange(id,field='status')
    {
        $.post("{:url('/power/role_fieldchange')}",{id:id,field:field},function(){});
    }
</script>
